
package com.scalar.chiptrack.rma.actions;


import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.rma.dao.RmaDAO;
import com.scalar.chiptrack.rma.forms.RMAListForm;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.users.User;

public final class RMAListAction extends ChipTrackAction
{
   private String customerName = null;
   private ArrayList customersList = new ArrayList();
   public static String ACTION_NAME = "/rma.do";

   public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception 
   {

		RMAListForm subform = ( RMAListForm ) form;
		String action = request.getParameter( "action" );
		HttpSession session = request.getSession();
        User user = ( User )session.getAttribute( SessionKeys.USER_KEY );

        if( action == null ) 
        {
			action = "GetAll";
		}
        
        if( user == null )
        {
	    	session.setAttribute (SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString());
            return (mapping.findForward ("login"));
        }
        String showModify = "FALSE"; String showDelete = "FALSE"; String showView = "FALSE";

       if( user.isActionAllowed( Functionality.RMA_FORM, User.USER_ACTION_MODIFY ) )
        {
             showModify = "TRUE";
        }
        if(user.isActionAllowed( Functionality.RMA_FORM, User.USER_ACTION_DELETE ) )
        {
             showDelete = "TRUE";
        }

        request.setAttribute( "showModify", showModify);
        request.setAttribute( "showDelete", showDelete);
                
		HashMap errors = new HashMap();
		
        customersList = CustomerDAO.getAllCustomerNames();

	    if( "GenerateRMAForm".equals( action ) )
        {
			request.setAttribute("showList", "TRUE");
			request.setAttribute("customers", customersList);
		}
		       
        if( "Search".equalsIgnoreCase( action ) )
        {
			String customerId   = (String) BeanUtils.getSimpleProperty( form, "customerName" );
			String rmaNumber    = (String) BeanUtils.getSimpleProperty( form, "rmaNumber" );
			String status       = (String) request.getParameter( "rmaStatus" );

            ArrayList rmaList = new ArrayList();
            rmaList = RmaDAO.searchRmaDetails( rmaNumber, customerId, status );

            if( rmaList.size() == 0 )
            {
                errors.put( ERROR_RMA_NOT_FOUND_WITH_GIVEN_SEARCH_CRITERIA , null );
                request.setAttribute( "showTable", "FALSE" );
            }
            else
            {
                request.setAttribute( "rmaList", rmaList );
                request.setAttribute( "showTable", "TRUE" );
            }

			request.setAttribute("customers", customersList );
        }

        if( "rmaReport".equals( action ) )
        {

			request.setAttribute("mktgPartNumberSelectList", FinishedGoodsDAO.loadAllFinishedGoods() );
            request.setAttribute("customerSelectList", customersList);

            return ( mapping.findForward("rmaReport" ) );
 		}
		// Report any errors we have discovered back to the original form
		if ( !errors.isEmpty() )
        {
			request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
		}

        request.setAttribute( "customerslist", customersList );

        // Forward control to the specified success URI
        return( mapping.findForward( "success" ) );

    }

}
